

import { config } from "../../utils/config";
import { request, sendRequest } from "../../utils/request";
import { ResponseData, RequestHeader } from "../types";
import { LoginReq, LoginRes, PhonenumberReq, RefreshTokenReq, TokenRes, RolesPerms, UserInfo } from "./types";
import { db } from "../../utils/db";

// 认证API实现.
class AuthApiImpl {

  //code 登录
  miniappLogin(data: LoginReq): Promise<ResponseData<LoginRes>> {
    return request({
      url: "/v1/front/rbac/miniapp_login/" + config.tenant,
      method: "POST",
      data
    })
  }

  // 手机号注册登录
  miniappPhoneNumber(data: PhonenumberReq): Promise<ResponseData<LoginRes>> {
    return request({
      url: "/v1/front/rbac/miniapp_phonenumber/" + config.tenant,
      method: "POST",
      data
    })
  }

  // 刷新token
  refreshToken(data: RefreshTokenReq): Promise<ResponseData<TokenRes>> {
    data.tenant = config.tenant;
    data.clientId = config.appId;
    // 请求header
    const header: RequestHeader = {
      "content-type": "application/json"
    }
    return sendRequest({
      url: "/v1/front/rbac/auth/refresh_token",
      method: "POST",
      data
    }, header);
  }

  // 登出
  logout(accessToken: string) {
    return request({
      url:
        "/v1/rbac/auth/logout?tenant=" +
        config.tenant +
        "&client_id=" +
        config.appId +
        "&accessToken=" +
        accessToken,
      method: "DELETE"
    });
  }

  // 获取当前会话用户
  userInfo(): Promise<ResponseData<UserInfo>> {
    return request({
      url: "/v1/rbac/auth/user?tenant=" + config.tenant,
      method: "GET"
    });
  }

  // 当前用户角色权限
  userRolesPerms(): Promise<ResponseData<RolesPerms>> {
    return request({
      url: "/v1/rbac/auth/roles_perms?tenant=" + config.tenant,
      method: "GET"
    });
  }

  // 允许切换身份的用户列表
  allowRunAsUser() {
    return request({
      url: "/v1/rbac/auth/allow_run_as_user?tenant=" + config.tenant,
      method: "GET"
    });
  }

  // 运行某种身份
  runAsUser(userId: string) {
    const data = {
      tenant: config.tenant,
      userId: userId
    };
    return request({
      url: "/v1/rbac/auth/run_as",
      method: "POST",
      data: data
    });
  }

  // 更新用户
  updateUser(data: any) {
    return request({
      url: "/v1/rbac/auth/user",
      method: "PATCH",
      data: data
    });
  }

  //修改密码
  updatePwd(encodeOldPwd: string, encodeNewPwd: string) {
    const data = {
      tenant: db.getUserTenant(),
      encodeOldPwd: encodeOldPwd,
      encodeNewPwd: encodeNewPwd
    };
    return request({
      url: "/v1/rbac/auth/change_pwd",
      method: "PATCH",
      data: data
    });
  }

  // 部门Tree 
  deptTree(deptId?: string) {
    const query = {
      tenant: db.getUserTenant(),
      excludeId: deptId
    };
    return request({
      url: "/v1/rbac/dept/treelist",
      method: "GET",
      data: query
    });
  }

  // 职务Tree
  jobTree() {
    const query = {
      tenant: db.getUserTenant(),
      roleType: "job",
      hasRoleType: true
    };
    return request({
      url: "/v1/rbac/role/treelist",
      method: "GET",
      data: query
    });
  }

}

const authApi = new AuthApiImpl();

export { authApi };


